﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using FinMkts.CFD.Data;

namespace FinMkts.CFD.Rest
{
    // Start the service and browse to http://<machine_name>:<port>/CFD/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [XmlSerializerFormat]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class CfdService
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [Description("Get a summary list of all known market places")]
        [WebGet(UriTemplate = "")]
        public List<MarketPlaceDC> GetMarketPlaces()
        {
            using (var context = new CfdModelContainer())
            {
                var marketPlaces = from mp in context.MarketPlaces
                                   select new MarketPlaceDC {Code = mp.Code, Name = mp.Name};
                return marketPlaces.ToList();
            }
        }

        [Description("Get a summary list of markets listed in a market place")]
        [WebGet(UriTemplate = "{marketPlace}/")]
        public List<CfdMarketDC> GetMarkets(string marketPlace)
        {
            using (var context = new CfdModelContainer())
            {
                var listings = from mp in context.MarketPlaces
                               where mp.Code == marketPlace
                               join ml in context.MarketListings on mp.Id equals ml.MarketPlaceId
                               select new CfdMarketDC { ListingCode = ml.ListingCode, MarketPlace = mp.Code, Name = ml.Instrument.Name };
                return listings.ToList();
            }
        }

        //[WebInvoke(UriTemplate = "", Method = "POST")]
        //public CfdMarketDC Create(SampleItem instance)
        //{
        //    // TODO: Add the new instance of SampleItem to the collection
        //    throw new NotImplementedException();
        //}

        [Description("Get a summary of a particular market listed in a market place")]
        [WebGet(UriTemplate = "{marketPlace}/{listingCode}")]
        public CfdMarketDC Get(string marketPlace, string listingCode)
        {
            using (var context = new CfdModelContainer())
            {
                var listings = from mp in context.MarketPlaces
                               where mp.Code == marketPlace
                               join ml in context.MarketListings on mp.Id equals ml.MarketPlaceId
                               where ml.ListingCode == listingCode
                               select new CfdMarketDC { ListingCode = ml.ListingCode, MarketPlace = mp.Code, Name = ml.Instrument.Name };
                return listings.FirstOrDefault();
            }
        }

        //[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
        //public SampleItem Update(string id, SampleItem instance)
        //{
        //    // TODO: Update the given instance of SampleItem in the collection
        //    throw new NotImplementedException();
        //}

        //[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        //public void Delete(string id)
        //{
        //    // TODO: Remove the instance of SampleItem with the given id from the collection
        //    throw new NotImplementedException();
        //}

    }
}
